﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;

namespace LED_Tester
{
    public partial class frmManual : Form
    {
        public frmManual()
        {
            InitializeComponent();
        }

        #region 电源变量

        private ClassSourceMeter classSourceMeter;
        private int sourceMode;
        private SerialPort sptSource;
        private bool sourceIsJoin;
        private double voltSet;
        private double currSet;

        public ClassSourceMeter Class_SourceMeter
        {
            get { return classSourceMeter; }
            set { classSourceMeter = value; }
        }
        public int SourceMode
        {
            get { return sourceMode; }
            set { sourceMode = value; }
        }
        public SerialPort SPTSource
        {
            get { return sptSource; }
            set { sptSource = value; }
        }
        public bool SourceIsJoin
        {
            get { return sourceIsJoin; }
            set { sourceIsJoin = value; }
        }
        public double VoltSet
        {
            get { return voltSet; }
            set { voltSet = value; }
        }
        public double CurrSet
        {
            get { return currSet; }
            set { currSet = value; }
        }

        #endregion

        #region 控制卡变量

        private bool ctrlBoardIsJoin;
        private const int XCH = 0;
        private const int YCH = 1;
        private const int ZCH = 2;
        private int strVelXCH;
        private int maxVelXCH;
        private int strVelYCH;
        private int maxVelYCH;
        private int strVelZCH;
        private int maxVelZCH;
        private double tacc;
        private double impulseXCH;
        private double impulseYCH;
        private double impulseZCH;

        public bool CtrlBoardIsJoin
        {
            get { return ctrlBoardIsJoin; }
            set { ctrlBoardIsJoin = value; }
        }
        public int StrVelXCH
        {
            get { return strVelXCH; }
            set { strVelXCH = value; }
        }
        public int MaxVelXCH
        {
            get { return maxVelXCH; }
            set { maxVelXCH = value; }
        }
        public int StrVelYCH
        {
            get { return strVelYCH; }
            set { strVelYCH = value; }
        }
        public int MaxVelYCH
        {
            get { return maxVelYCH; }
            set { maxVelYCH = value; }
        }
        public int StrVelZCH
        {
            get { return strVelZCH; }
            set { strVelZCH = value; }
        }
        public int MaxVelZCH
        {
            get { return maxVelZCH; }
            set { maxVelZCH = value; }
        }
        public double Tacc
        {
            get { return tacc; }
            set { tacc = value; }
        }
        public double ImpulseXCH
        {
            get { return impulseXCH; }
            set { impulseXCH = value; }
        }
        public double ImpulseYCH
        {
            get { return impulseYCH; }
            set { impulseYCH = value; }
        }
        public double ImpulseZCH
        {
            get { return impulseZCH; }
            set { impulseZCH = value; }
        }

        #endregion

        #region 光谱仪变量

        private ClassSpectralMeter classSpectralMeter;
        private ClassSpectralMeter.Param param;
        private ClassSpectralMeter.Result result;
        private bool spectralIsJoin;

        public ClassSpectralMeter Class_spectralMeter
        {
            get { return classSpectralMeter; }
            set { classSpectralMeter = value; }
        }
        public ClassSpectralMeter.Param Param
        {
            get { return param; }
            set { param = value; }
        }
        public ClassSpectralMeter.Result Result
        {
            get { return result; }
            set { result = value; }
        }
        public bool SpectralIsJoin
        {
            get { return spectralIsJoin; }
            set { spectralIsJoin = value; }
        }

        #endregion

        #region 光强板变量

        private ClassLuxBoard classLuxBoard;
        private bool luxIsJoin;
        private double distance;
        private double luxStanValue;
        private double luxDarkValue;


        public ClassLuxBoard Class_LuxBoard
        {
            get { return classLuxBoard; }
            set { classLuxBoard = value; }
        }
        public bool LuxIsJoin
        {
            get { return luxIsJoin; }
            set { luxIsJoin = value; }
        }
        public double Distance
        {
            get { return distance; }
            set { distance = value; }
        }
        public double LuxDarkValue
        {
            get { return luxDarkValue; }
            set { luxDarkValue = value; }
        }

        #endregion

        #region 其它变量

        private double[] secondParam;
        private double luxParam;

        public double[] SecondParam
        {
            get { return secondParam; }
            set { secondParam = value; }
        }
        public double LuxParam
        {
            get { return luxParam; }
            set { luxParam =value;}
        }

        #endregion

        #region 功能函数

        private static void TxtDataVerifyWithOnePoint(KeyPressEventArgs e, TextBox txt)
        {
            int pointCount = 0;
            bool isPointExist = false;
            if (char.IsNumber(e.KeyChar) || e.KeyChar == '.' || e.KeyChar == (char)Keys.Back)
            {
                e.Handled = false;
                if (txt.Text.Length == 0)
                {
                    if (e.KeyChar == '.')
                        e.Handled = true;
                }

                for (int i = 0; i < txt.Text.Length; i++)
                {
                    if (txt.Text.Substring(i, 1) == ".")
                        pointCount++;
                    if (pointCount == 1)
                        isPointExist = true;
                }

                if (isPointExist && e.KeyChar == '.')
                    e.Handled = true;
            }

            else { e.Handled = true; }
        }

        private static void TxtDataVerifyWithOnePoint2(KeyPressEventArgs e, TextBox txt)
        {
            int pointCount = 0;
            bool isPointExist = false;
            if (char.IsNumber(e.KeyChar) || e.KeyChar == '.' || e.KeyChar == (char)Keys.Back || e.KeyChar == '-')
            {
                e.Handled = false;
                if (txt.Text.Length == 0)
                {
                    if (e.KeyChar == '.')
                        e.Handled = true;
                }

                for (int i = 0; i < txt.Text.Length; i++)
                {
                    if (txt.Text.Substring(i, 1) == ".")
                        pointCount++;
                    if (pointCount == 1)
                        isPointExist = true;
                }

                if (isPointExist && e.KeyChar == '.')
                    e.Handled = true;
            }

            else { e.Handled = true; }
        }

        #endregion

        #region 窗体事件

        private void frmManual_Load(object sender, EventArgs e)
        {
            if (!sourceIsJoin)
            {
                MessageBox.Show("电源未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                txtVoltage.Text = voltSet.ToString("#0.0000");
                txtCurrent.Text = currSet.ToString("#0.0000");
            }
            if (!ctrlBoardIsJoin)
            {
                MessageBox.Show("控制卡未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                txtXCHNow.Text = ((double)((ClassCtrlBorad.d1000_get_command_pos(XCH) + (int)impulseXCH * 140) / (int)impulseXCH) * -1).ToString("#0.0");
                txtYCHNow.Text = (ClassCtrlBorad.d1000_get_command_pos(YCH) / impulseYCH).ToString("#0.0");
            }
            if (!spectralIsJoin)
            {
                MessageBox.Show("光谱仪未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                if (param.IsAutoAdjust)
                {
                    checkBox1.Checked = true;
                    nudIntTime.Enabled = false;
                    nudIntTime.Value = param.IntTime;
                }
                else
                {
                    checkBox1.Checked = false;
                    nudIntTime.Enabled = true;
                    nudIntTime.Value = param.IntTime;
                }
            }
            //if (!luxIsJoin)
            //{
            //    MessageBox.Show("光强板未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //}
        }

        #endregion

        #region 按钮事件

        private void btnLightOn_Click(object sender, EventArgs e)
        {
            if (sourceIsJoin)
            {
                voltSet = double.Parse(txtVoltage.Text);
                currSet = double.Parse(txtCurrent.Text);
                classSourceMeter.Set(sourceMode, sptSource, voltSet, currSet);
                classSourceMeter.LightOn(sourceMode, sptSource);
            }
            else
            {
                MessageBox.Show("电源未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnLightOff_Click(object sender, EventArgs e)
        {
            if (sourceIsJoin)
            {
                classSourceMeter.LightOff(sourceMode, sptSource);
            }
            else
            {
                MessageBox.Show("电源未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnRadiant_Click(object sender, EventArgs e)
        {
            if (spectralIsJoin)
            {
                param.IntTime = (int)nudIntTime.Value;

                classSpectralMeter.Measure(param, 380d, 830d, ref result);

                result.x += secondParam[1];
                result.y += secondParam[2];
                result.colorTemp += secondParam[3];

                txtSmallx.Text = result.x.ToString("#0.0000");
                txtSmally.Text = result.y.ToString("#0.0000");
                txtColorTemp.Text = result.colorTemp.ToString("#0");
            }
            else
            {
                MessageBox.Show("光谱仪未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnLuminous_Click(object sender, EventArgs e)
        {
            if (spectralIsJoin)
            {
                classSpectralMeter.MeasureCandela(param, ref luxStanValue);
                luxStanValue *= luxParam;
                txtCandela.Text = luxStanValue.ToString("0.000");
            }
            else
            {
                MessageBox.Show("光谱仪未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnMove_Click(object sender, EventArgs e)
        {
            if (ctrlBoardIsJoin)
            {
                //if (txtYCHNew.Text == "" || txtXCHNew.Text == "")
                if(double.Parse(txtYCHNew.Text) <0 || double.Parse(txtYCHNew.Text) >180||double.Parse(txtXCHNew.Text) <-140||double.Parse(txtXCHNew.Text)>140)
                {
                    MessageBox.Show("请输入正确数值！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                try
                {
                    int pos = (int)(double.Parse(txtYCHNew.Text) * impulseYCH);
                    ClassCtrlBorad.d1000_start_ta_move(YCH, pos, strVelYCH, maxVelYCH, tacc);
                    while (ClassCtrlBorad.d1000_check_done(YCH) == 0)
                    {
                        Thread.Sleep(1);
                    }
                }
                catch
                {
                    MessageBox.Show("请输入正确数值！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
               

                try
                {
                    int pos = (int)((double.Parse(txtXCHNew.Text) + 140) * (int)impulseXCH) * (-1);
                    ClassCtrlBorad.d1000_start_ta_move(XCH, pos, strVelXCH, maxVelXCH, tacc);
                    while (ClassCtrlBorad.d1000_check_done(XCH) == 0)
                    {
                        Thread.Sleep(1);
                    }
                }
                catch
                {
                    MessageBox.Show("请输入正确数值！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                MessageBox.Show("移动完成！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtXCHNow.Text = ((double)((ClassCtrlBorad.d1000_get_command_pos(XCH) + (int)impulseXCH * 140) / impulseXCH) * -1).ToString("#0.0");
                txtYCHNow.Text = (ClassCtrlBorad.d1000_get_command_pos(YCH) / impulseYCH).ToString("#0.0");
            }
            else
            {
                MessageBox.Show("控制卡未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        #endregion

        #region 其它事件

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                param.IsAutoAdjust = true;
                nudIntTime.Enabled = false;
            }
            else
            {
                param.IsAutoAdjust = false;
                nudIntTime.Enabled = true;
            }
        }

        private void txtVoltage_KeyPress(object sender, KeyPressEventArgs e)
        {
            TxtDataVerifyWithOnePoint(e, txtVoltage);
        }

        private void txtCurrent_KeyPress(object sender, KeyPressEventArgs e)
        {
            TxtDataVerifyWithOnePoint(e, txtCurrent);
        }

        private void txtYCHNew_KeyPress(object sender, KeyPressEventArgs e)
        {
            TxtDataVerifyWithOnePoint2(e, txtYCHNew);
        }

        private void txtXCHNew_KeyPress(object sender, KeyPressEventArgs e)
        {
            TxtDataVerifyWithOnePoint2(e, txtXCHNew);
        }

        #endregion

        private void btnWatt_Click(object sender, EventArgs e)
        {
            if (spectralIsJoin)
            {
                Class_spectralMeter.Measure(param, 380d, 830d, ref result);
                result.uWatt *= luxParam;
                txtWatt.Text = result.uWatt.ToString("0.000");

            }
            else
            {
                MessageBox.Show("光谱仪未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
